package com.xinlu.common.game.bo;

import java.lang.reflect.InvocationTargetException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import com.xinlu.common.game.dao.BankBackDao;
import com.xinlu.common.game.value.BankBackVo;
import com.xinlu.common.game.value.TdbbVo;
import com.xinlu.common.game.value.ZjmxVo;
import com.xinlu.derek.common.BaseBo;
import com.xinlu.persistence.BankBack;
import com.xinlu.persistence.Users;
import com.xinlu.user.dao.UsersDao;
import com.xinlu.util.tool.DateUtil;
import com.xinlu.util.tool.PageObject;

public class BankBackBo extends BaseBo {

	private BankBackDao bankBackDao;

	public void setBankBackDao(BankBackDao bankBackDao) {
		this.bankBackDao = bankBackDao;
	}

	private UsersDao usersDao;

	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}

	public void saveTzjlIntoBankBack(String username, String type, double addmoney, double oldmoney, double nowmoney, String pro_num, String cpnum, int bz, String states) {

		this.bankBackDao.saveTzjlInfoInBankBack(username, type, addmoney, oldmoney, nowmoney, pro_num, cpnum, bz, states);
	}

	/**
	 * 获取已经取款数
	 * 
	 * @param username
	 * @return
	 */
	public int getYjqk(String username) {

		String date = DateUtil.getFormatDate(DateUtil.YYYY_MM_DD, new Date());
		return bankBackDao.getYjqk(username,date+ ConstantLstBo.getBgnHMS(date),date+ ConstantLstBo.getEndHMS(date));
	}

	/**
	 * 发起提现
	 * 
	 * @param tqzj
	 * @param crtUser
	 */
	public void fqtx(Double tqzj, Users crtUser) {
		BankBack bb = new BankBack();
		bb.setLotteryid("QK-" + DateUtil.getFormatDate(DateUtil.YYYYMMDDHHMI, new Date()));
		bb.setBBefor(crtUser.getUermoney());
		bb.setBAfter(crtUser.getUermoney() - tqzj);
		bb.setBackMoney(tqzj);
		bb.setBackMoney2(0D);
		bb.setType("取款");
		bb.setFollows("发起提现");
		bb.setExpect("");
		bb.setUsername(crtUser.getUsername());
		bb.setAddtime(DateUtil.getTimestamp(new Date()));
		bb.setProstate("正常");
		bb.setState(0);
		bankBackDao.save(bb);
	}

	/**
	 * 加载流水账列表
	 * 
	 * @param caiZhong
	 * @param lszLeixing
	 * @param bgnDate
	 * @param endDate
	 * @return
	 */
	public PageObject loadCqklst(String yhm, String lszLeixing, String bgnDate, String endDate, int pageNo) {

		PageObject po = bankBackDao.loadCqklst(yhm, lszLeixing, bgnDate, endDate, pageNo);
		List lst = po.getItems();
		List cqkLst = new ArrayList<ZjmxVo>();

		if (lst != null) {
			for (int i = 0; i < lst.size(); i++) {

				ZjmxVo vo = new ZjmxVo();
				BankBack bankBack = (BankBack) lst.get(i);
				vo.setAllmoney(ConstantLstBo.getDoubleValue(bankBack.getBAfter()));
				vo.setChangeMoney(ConstantLstBo.getDoubleValue(bankBack.getBackMoney2()) - ConstantLstBo.getDoubleValue(bankBack.getBackMoney()));
				vo.setCkmx(0);
				vo.setDgsj(DateUtil.getFormatDate(DateUtil.OYYYY_MM_DD_HH_MI, bankBack.getAddtime()));
				vo.setDzh(bankBack.getLotteryid());
				vo.setFollows(bankBack.getFollows());
				vo.setQh(bankBack.getExpect());
				vo.setState(bankBack.getProstate());
				vo.setWfjczhw("");
				cqkLst.add(vo);
			}
		}

		po.setItems(cqkLst);
		return po;
	}

	/**
	 * 加载投注记录列表
	 * 
	 * @param caiZhong
	 * @param lszLeixing
	 * @param bgnDate
	 * @param endDate
	 * @return
	 */
	public PageObject loadTzjlLst(String yhm, String caiZhong, String lszLeixing, String bgnDate, String endDate, int pageNo) {

		PageObject po = bankBackDao.loadTzlst(yhm, caiZhong, lszLeixing, bgnDate, endDate, pageNo);
		List lst = po.getItems();
		List tzLst = new ArrayList<ZjmxVo>();

		if (lst != null) {
			for (int i = 0; i < lst.size(); i++) {

				ZjmxVo vo = new ZjmxVo();
				BankBackVo bankBack = (BankBackVo) lst.get(i);

				vo.setAllmoney(ConstantLstBo.getDoubleValue(bankBack.getBAfter()));
				vo.setChangeMoney(ConstantLstBo.getDoubleValue(bankBack.getBackMoney2()) - ConstantLstBo.getDoubleValue(bankBack.getBackMoney()));
				vo.setCkmx(1);
				vo.setDgsj(DateUtil.getFormatDate(DateUtil.OYYYY_MM_DD_HH_MI, bankBack.getAddtime()));
				String dzh = bankBack.getLotteryid();
				if (StringUtils.isNotEmpty(dzh) && dzh.length() > 20) {
					vo.setDzh(dzh.substring(0, 20) + "...");
				} else {
					vo.setDzh(dzh);
				}
				vo.setFollows(bankBack.getFollows());
				vo.setQh(bankBack.getExpect());
				vo.setState(bankBack.getProstate());
				vo.setWfjczhw(ConstantLstBo.getwfjcNotContainCaiZhong(bankBack.getWfjczwm()));
				tzLst.add(vo);
			}
		}

		po.setItems(tzLst);
		return po;
	}

	/**
	 * 加载团队报表
	 * 
	 * @param crtUser
	 * @param caiZhong
	 * @param bgnDate
	 * @param endDate
	 * @return
	 */
	public List loadTdbbs(Users crtUser, String caiZhong, String bgnDate, String endDate) {

		List tdbbs = new ArrayList();
		List<Users> xyjyhs = usersDao.loadXyjyhs(crtUser);// 当前用户的下一级,不包含自己
		for (int i = 0; i < xyjyhs.size(); i++) {
			
			// 获取下一级用户的下级用户报表
			List<Users> dqyhXyjyhs = usersDao.loadXyjyhs(xyjyhs.get(i));
			dqyhXyjyhs.add(xyjyhs.get(i));
			List dqyhXyjyhbbs = new ArrayList();
			for(int j=0;j<dqyhXyjyhs.size();j++){
				TdbbVo xjyhbbVo = loadYhbb(dqyhXyjyhs.get(j), caiZhong, bgnDate, endDate);
				if (xjyhbbVo != null) {
					dqyhXyjyhbbs.add(xjyhbbVo);
				}
			}
			
			TdbbVo xyjbbVo = loadBbzj(dqyhXyjyhbbs);
			if(xyjbbVo!=null){
				tdbbs.add(xyjbbVo);
			}
		}

		return tdbbs;
	}

	/**
	 * 加载用户报表
	 * 
	 * @param crtUser
	 * @param caiZhong
	 * @param bgnDate
	 * @param endDate
	 * @return
	 */
	public TdbbVo loadYhbb(Users crtUser, String caiZhong, String bgnDate, String endDate) {

		TdbbVo bbVo = new TdbbVo();
		String yhm = crtUser.getUsername();
		if (ConstantLstBo.isNullOrEmpty(yhm)) {
			return null;
		}
		
		bbVo.setCk(ConstantLstBo.getDoubleValue(bankBackDao.loadTdbbCqk(yhm, "存款", bgnDate, endDate)));
		bbVo.setFd(ConstantLstBo.getDoubleValue(bankBackDao.loadTdbbTzjl(yhm, caiZhong, "返点", bgnDate, endDate)));
		bbVo.setQk(ConstantLstBo.getDoubleValue(bankBackDao.loadTdbbCqk(yhm, "取款", bgnDate, endDate)));
		bbVo.setPj(ConstantLstBo.getDoubleValue(bankBackDao.loadTdbbTzjl(yhm, caiZhong, "派奖", bgnDate, endDate)));
		bbVo.setXf(ConstantLstBo.getDoubleValue(bankBackDao.loadTdbbTzjl(yhm, caiZhong, "代购", bgnDate, endDate)));
		
		// 过滤没有账变的记录
		if(!(bbVo.getCk()>0 || bbVo.getFd()>0 || bbVo.getQk()>0 || bbVo.getPj()>0 || bbVo.getXf()>0)){
			return null;
		}

		// 采用格式化
		NumberFormat formatter = NumberFormat.getNumberInstance();
		formatter.setMaximumIntegerDigits(2);

		bbVo.setYl(ConstantLstBo.getDoubleValue(bbVo.getPj() + bbVo.getFd() - bbVo.getXf())); // 派奖+返点-代购
		bbVo.setSsz(crtUser.getDaili() == 1 ? "代理" : "会员");
		bbVo.setYhm(yhm);

		return bbVo;
	}

	/**
	 * 加载团队报表总额
	 * 
	 * @param tdbbs
	 * @return
	 */
	public TdbbVo loadBbzj(List tdbbs) {
		
		if(tdbbs==null || tdbbs.size()==0){
			return null;
		}
		
		TdbbVo vo = new TdbbVo();

		for (int i = 0; i < tdbbs.size(); i++) {
			TdbbVo tdbbVo = (TdbbVo) tdbbs.get(i);
			if(tdbbVo==null){
				continue;
			}
			vo.setCk(ConstantLstBo.getDoubleValue(vo.getCk()) + ConstantLstBo.getDoubleValue(tdbbVo.getCk()));
			vo.setXf(ConstantLstBo.getDoubleValue(vo.getXf()) + ConstantLstBo.getDoubleValue(tdbbVo.getXf()));
			vo.setFd(ConstantLstBo.getDoubleValue(vo.getFd()) + ConstantLstBo.getDoubleValue(tdbbVo.getFd()));
			vo.setPj(ConstantLstBo.getDoubleValue(vo.getPj()) + ConstantLstBo.getDoubleValue(tdbbVo.getPj()));
			vo.setQk(ConstantLstBo.getDoubleValue(vo.getQk()) + ConstantLstBo.getDoubleValue(tdbbVo.getQk()));
			vo.setYl(ConstantLstBo.getDoubleValue(vo.getYl()) + ConstantLstBo.getDoubleValue(tdbbVo.getYl()));
			vo.setSsz(tdbbVo.getSsz());
			vo.setYhm(tdbbVo.getYhm());
		}

		return vo;
	}

	/**
	 * 加载团队流水账
	 * 
	 * @param crtUser
	 * @param dzh
	 * @param qh
	 * @param lszLeiXing
	 * @param xjyhm
	 * @param bgnDate
	 * @param endDate
	 * @return
	 */
	public PageObject loadTdLsz(Users crtUser, String dzh, String qh, String lszLeiXing, String xjyhm, String bgnDate, String endDate, int pageNo) {

		List<String> yhms = new ArrayList<String>();
		// 如果下级用户名为空，则加载出当前用户下的所有子用户
		if (ConstantLstBo.isNullOrEmpty(xjyhm)) {
			List<Users> yhs = usersDao.loadXjyhs(crtUser);
			for (Users user : yhs) {
				if (!ConstantLstBo.isNullOrEmpty(user.getUsername())) {
					yhms.add(user.getUsername());
				}
			}
		} else {
			yhms.add(xjyhm);
		}

		PageObject po = bankBackDao.loadTdLsz(yhms, dzh, qh, lszLeiXing, bgnDate, endDate, pageNo);
		return po;
	}

	/**
	 * 撤单
	 * 
	 * @param crtUser
	 * @param dzh
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public Double cheDan(Users crtUser, String dzh) throws IllegalAccessException, InvocationTargetException {

		BankBack bb = bankBackDao.loadLsz(dzh);
		bb.setProstate("已撤单");
		bankBackDao.update(bb);

		BankBack newBb = new BankBack();
		BeanUtils.copyProperties(newBb, bb);
		newBb.setAddtime(DateUtil.getTimestamp(new Date()));
		newBb.setBackMoney(0D);
		newBb.setBackMoney2(ConstantLstBo.getDoubleValue(bb.getBackMoney()));
		newBb.setBBefor(crtUser.getUermoney());
		newBb.setBAfter(ConstantLstBo.getDoubleValue(newBb.getBBefor()) + ConstantLstBo.getDoubleValue(newBb.getBackMoney2()));
		newBb.setFollows("用户撤单:" + newBb.getExpect());
		newBb.setProstate("已撤单");
		bankBackDao.save(newBb);

		return bb.getBackMoney();
	}

	/**
	 * 充值
	 * 
	 * @param crtUser
	 * @param xjyh
	 * @param czje
	 */
	public void chongZhi(Users crtUser, Users xjyh, Double czje) {

		BankBack crtBankBack = new BankBack();
		crtBankBack.setAddtime(DateUtil.getTimestamp(new Date()));
		crtBankBack.setBackMoney(czje);
		crtBankBack.setBackMoney2(0D);
		crtBankBack.setBAfter(crtUser.getUermoney());
		crtBankBack.setBBefor(crtUser.getUermoney() + czje);
		crtBankBack.setExpect("");
		crtBankBack.setFollows("给下级充值");
		crtBankBack.setLotteryid("QK-" + DateUtil.getFormatDate(DateUtil.YYYYMMDDHHMI, crtBankBack.getAddtime()));
		crtBankBack.setProstate("正常");
		crtBankBack.setState(1);
		crtBankBack.setType("取款");
		crtBankBack.setUsername(crtUser.getUsername());

		BankBack xjBankBack = new BankBack();
		xjBankBack.setAddtime(crtBankBack.getAddtime());
		xjBankBack.setBackMoney(0D);
		xjBankBack.setBackMoney2(czje);
		xjBankBack.setBAfter(xjyh.getUermoney());
		xjBankBack.setBBefor(xjyh.getUermoney() - czje);
		xjBankBack.setExpect("");
		xjBankBack.setFollows("上级充值");
		xjBankBack.setLotteryid("CK-" + DateUtil.getFormatDate(DateUtil.YYYYMMDDHHMI, crtBankBack.getAddtime()));
		xjBankBack.setProstate("正常");
		xjBankBack.setState(1);
		xjBankBack.setType("存款");
		xjBankBack.setUsername(xjyh.getUsername());

		bankBackDao.save(crtBankBack);
		bankBackDao.save(xjBankBack);

	}
	
}
